package com.dflc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dflc.entity.po.TFileAttachmentData;
import com.dflc.entity.rep.HttpResponseEntity;
import com.dflc.entity.vo.TAudioDataVo;
import com.dflc.mapper.TFileAttachmentDataMapper;
import com.dflc.service.TFileAttachmentDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 附件信息表 服务类
 * </p>
 *
 * @author mybatis-gc
 * @since 2024-04-03
 */
@Service
@Slf4j
public class TFileAttachmentDataServiceImpl extends ServiceImpl<TFileAttachmentDataMapper, TFileAttachmentData> implements TFileAttachmentDataService {

    @Resource
    private TFileAttachmentDataMapper tFileAttachmentDataMapper;

    @Override
    @Async
    public HttpResponseEntity deleteFile(List<Integer> fileIds) {
        for (Integer id : fileIds) {
            TFileAttachmentData audioData = this.getById(id);
            if (audioData == null) {
                return HttpResponseEntity.error(500, "没找到该文件的信息");
            }
            if (this.removeById(id)) {
                File file = new File(audioData.getUrl());
                if (file.exists()) {
                    if (file.delete()) {
                        log.info("删除文件源成功");
                    } else {
                        log.info("删除文件源失败");
                        return HttpResponseEntity.error(500, "删除文件源失败");
                    }
                }
            } else {
                return HttpResponseEntity.error(500, "删除文件数据失败");
            }
        }
        return HttpResponseEntity.success("删除成功");
    }

    /**
     * 根据文件ID更新文件业务类型和业务ID，返回影响行数
     *
     * @param businessType
     * @param businessId
     * @param fileIds
     * @return
     * @author oyt
     * @date 2025/07/20
     */
    @Override
    public int updateBusinessTypeAndIdById(Integer businessType, Integer businessId, List<Integer> fileIds) {
        if (ObjectUtils.isEmpty(fileIds) || fileIds.size() == 0)
            return 0;
        return tFileAttachmentDataMapper.updateBusinessTypeAndIdById(businessType, businessId, fileIds);
    }

    /**
     * 根据业务类型，业务ID，页面提交的ID列表，逻辑删除文件
     *
     * @param businessType
     * @param businessId
     * @param fileIds
     * @return
     * @author oyt
     * @date 2025/07/20
     */
    @Override
    public int deleteByIdNotIn(Integer businessType, Integer businessId, List<Integer> fileIds) {
        if (ObjectUtils.isEmpty(fileIds) || fileIds.size() == 0 || businessType == null || businessId == null)
            return 0;
        //查询出需要删除的文件
        List<TFileAttachmentData> tAudioDataList = tFileAttachmentDataMapper.selectByIdNotIn(businessType, businessId, fileIds);
        if (!ObjectUtils.isEmpty(tAudioDataList) && tAudioDataList.size() > 0){
            int deletedBatchIds = tFileAttachmentDataMapper.deleteBatchIds(tAudioDataList);
            //删除文件源
            for (TFileAttachmentData ttAudioData : tAudioDataList) {
                File file = new File(ttAudioData.getUrl());
                if (file.exists())
                    file.delete();
            }
            return deletedBatchIds;
        }
        return 0;
    }

    @Override
    public Map<String, Object> findByBusinessTypeAndBusinessId(Integer businessType, Integer businessId) {
        List<TAudioDataVo> audioDataVos = tFileAttachmentDataMapper.findByBusinessTypeAndBusinessId(businessType, businessId);
        Map<String, Object> map = new HashMap<>();
        map.put("fileList", audioDataVos);
        String fileIds = "";
        if (!ObjectUtils.isEmpty(audioDataVos)) {
            for (int i = 0 ; i < audioDataVos.size(); i++) {
                if(i==0){
                    fileIds = audioDataVos.get(i).getId().toString();
                }else {
                    fileIds=fileIds+","+audioDataVos.get(i).getId().toString();
                }
            }
        }
        map.put("fileIds", fileIds);
        return map;
    }
}
